package common;

import java.util.ArrayList;
import java.util.List;

public class WorkingAlphaNode extends WorkingMemory{
	private AlphaNode alphaNode;
	private List<BethaNode> bethaNodes;	
	private String value;
	
	static int cont = 0;
	private int id;
	
	public WorkingAlphaNode(){
		bethaNodes = new ArrayList<BethaNode>();
		id = cont++;
	}
	public AlphaNode getAlphaNode() {
		return alphaNode;
	}
	public void setAlphaNode(AlphaNode alphaNode) {
		this.alphaNode = alphaNode;
	}
	public List<BethaNode> getBethaNodes() {
		return bethaNodes;
	}
	public void setBethaNodes(List<BethaNode> bethaNodes) {
		this.bethaNodes = bethaNodes;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	
	public void addBethaNode(BethaNode betha){
		betha.getWorkingParentAlphaNodes().add(this);
		this.bethaNodes.add(betha);
	}
	
	public boolean isEmptyBethaNodes(){
		return this.bethaNodes.isEmpty();
	}
	
	public String toString(){
		String aux = "(WorkingAlphaNode_"+id+")";
		aux += " value=" + value + "\n\t\t";		
		for(BethaNode betha : bethaNodes)
			aux += betha.toString();
		return aux;
	}
	
	public List<Node> updateState(String value){
		this.setValue(value);
		List<Node> rulesToFire = new ArrayList<Node>();
		for(BethaNode betha : bethaNodes){
			 List<Node> listAux = betha.updateState(this.getAlphaNode().getKey(), this.getValue());			 
			if(listAux != null)
				rulesToFire.addAll(listAux);
		}
		return rulesToFire;
	}
	
	public boolean contains(String key){		
		return alphaNode.getKey().equals(key);
	}
	
}
